﻿/**
 * 作者：Boss
 * 时间：2018-12-28
 * 邮箱：240447286@qq.com
 */
; (function (document) {
    let api = {
        afreshBuildItems:function(eleId,opts){//重新初始化

            let _ele = getElement(eleId);
            if(_ele.parentNode!==undefined && _ele.parentNode.childNodes!==undefined){
                let list=_ele.parentNode.childNodes;
                for (let i = 0; i < list.length; i++) {
                    if(list[i].className=="boss-select")
                    _ele.parentNode.removeChild(list[i]);
                }
            }
            this.init(eleId,opts);
            return api;
        },
        init: function (eleId, opts) {//初始化
            let options=Object.assign({itemClickCallBack:function(){}},opts);
            let _ele =  getElement(eleId);
            _ele.classList.add('boss-hide');
            let element = createElement('div', 'boss-select');
            let span = createElement('span', 'boss-caret');
            let selected = createElement('div', 'boss-selected');
            let select = createElement('div', 'boss-dropdown  boss-hide');
            let input = createElement('input', 'boss-input');
            let selectItems = createElement('div', 'boss-select-items');
            element.appendChild(span);
            element.appendChild(selected);
            select.appendChild(input);
            select.appendChild(selectItems);
            element.appendChild(select);
            buildItems(select,selected,selectItems,_ele,options);
            _ele.parentNode.appendChild(element);
            input.addEventListener('input', () => {
                
                //筛选数据
                search(selectItems,input.value);
            });
            input.addEventListener('click',()=>{
                select.classList.remove('boss-hide');
                search(selectItems,input.value);
            });
            selected.addEventListener('click',()=>{
                select.classList.remove('boss-hide');
            });
            document.addEventListener('click', (e) => {
                // console.log('点击任意位置');
                // console.log(e.target);
                // console.log(element);
                if (e.target!==selected && e.target!==span && e.target!==input && e.target!==element){
                    select.classList.add('boss-hide');
                }
                    
            });
            return api;
        }

    }
    function getElement(elementName) {
        let eleType = elementName.substr(0, 1);
        let result;
        switch (eleType) {
            case '#':
                result = document.getElementById(elementName.substring(1));
                break;
            case '.':
                result = document.getElementsByClassName(elementName.substring(1))[0];
                break;
            default:
                result = document.getElementsByName(elementName)[0];
        }
        //console.log(result);
        return result;
    }
    function search(selectItems,value) {
        if (isNull(value)) {
            for (let i = 0; i < selectItems.childNodes.length; i++) {
                selectItems.childNodes[i].classList.remove('boss-hide');
            }

        } else {
            for (let i = 0; i < selectItems.childNodes.length; i++) {
                if (selectItems.childNodes[i].innerText.toLowerCase().indexOf(value) > -1)
                    selectItems.childNodes[i].classList.remove('boss-hide');
                else
                    selectItems.childNodes[i].classList.add('boss-hide');
            }
        }
    }
    //构建数据项
    function buildItems(select,selected,selectItems,_ele,opts) {
        //清除之前的子级元素
        selectItems.innerHTML = '';
        let selectedIndex = _ele.selectedIndex;
        _ele.querySelectorAll('option').forEach((v, i) => {
            let text = v.innerText, value = v.getAttribute('value');
            let item = createElement('div', 'boss-select-item', text);
            item.setAttribute('data-value', value);
            if (selectedIndex == i) 
                dropdownSelected(selected,item,_ele);
            item.addEventListener('click', () => {
                dropdownSelected(selected,item,_ele);
                select.classList.add('boss-hide');
                
                if (typeof (opts.itemClickCallBack) == "function")
                opts.itemClickCallBack();
            });
            selectItems.appendChild(item);
        });
    }
    //选中
    function dropdownSelected(selected,item,_ele) {
        selected.innerText = item.innerText;
        let value = item.getAttribute('data-value');
        for (let i = 0; i < _ele.options.length; i++) {
            if (_ele.options[i].value == value) {
                _ele.options[i].selected = true;
                break;
            }
        }
    }
    function createElement(elementName, className, text) {
        let result = document.createElement(elementName);
        if (!isNull(className))
            result.className = className;
        if (!isNull(text))
            result.innerText = text;
        return result;
    }
    function isNull(value) {
        if (value == undefined || value == null || value == '')
            return true;
        return false
    }
    this.BossSelectorSearch = api;
})(document);